package com.mxchip.livestarmobile.mobile.ui.meeting

import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Switch
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.mxchip.livestarmobile.mobile.CallActivityA
import com.mxchip.livestarmobile.R
import com.mxchip.hxb3288a.base.hideKeyboard
import com.mxchip.livestarmobile.mobile.base.toast
import com.mxchip.livestarmobile.mobile.state.AppConstant
import com.mxchip.livestarmobile.mobile.ui.P2PSingleCall
import com.orhanobut.hawk.Hawk
import kotlinx.android.synthetic.main.fragment_meeting.*

class MeetingFragment : Fragment() {

    private val meetingViewModel by activityViewModels<MeetingViewModel>()

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_meeting, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // meetingViewModel.meetingId.observe(viewLifecycleOwner, Observer { s -> text_meeting.text = "$s 会议室" })
        // meetingViewModel.liveCount.observe(viewLifecycleOwner, Observer { i -> text_live_count.text = "${i}人 会议中" })
        // meetingViewModel.inviteIds.observe(viewLifecycleOwner, Observer { list -> text_invite_count.text = "${list.size}人" })

        button_enter.setOnClickListener {
            val termId = edit_text_term_id.text.toString()
            try {

                hideKeyboard()
                val call = meetingViewModel.makeCall(termId)
                if (call is P2PSingleCall) {
                    meetingViewModel.insertCallLog(call.number)
                } else {
                    meetingViewModel.insertCallLog(termId)
                }
                context?.let { CallActivityA.open(it, call) }
            } catch (t: Throwable) {
                t.message?.let { toast(it) }
            }
        }
        switch1.isChecked = Hawk.get(AppConstant.SP_CLOSE_CAMERA) ?: false
        switch2.isChecked = Hawk.get(AppConstant.SP_MUTE_AUDIO) ?: false
        switch1.setOnCheckedChangeListener { buttonView, isChecked ->
            Hawk.put(AppConstant.SP_CLOSE_CAMERA, isChecked)
        }
        switch2.setOnCheckedChangeListener { buttonView, isChecked ->
            Hawk.put(AppConstant.SP_MUTE_AUDIO, isChecked)
        }
        /*@OnClick(R.id.view)
        fun onInviteClick(view: View) {
            val intent = Intent(context, MeetingCallInviteActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE)
        }*/

        switch2.setOnClickListener {
            if (it is Switch)
                meetingViewModel.camera = it.isChecked
        }

        switch1.setOnClickListener {
            if (it is Switch)
                meetingViewModel.mic = it.isChecked
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        // super.onActivityResult(requestCode, resultCode, data);

        Log.e("MeetingFragment", "onActivityResult: requestCode: $requestCode, resultCode: $resultCode")

        if (requestCode == REQUEST_CODE) {

            if (resultCode == RESULT_OK) {

                val numbers = data!!.getStringArrayListExtra("numbers")

                Log.e("MeetingFragment", "data: $numbers")

                meetingViewModel.inviteIds.value = numbers

                return
            }
        }
    }


    companion object {
        private const val REQUEST_CODE = 0x11
    }
}